package controller;

import dao.GoodsDao;
import dao.OrderDao;
import dao.UserDao;
import domain.Order;
import domain.OrderGoods;
import domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import util.CompositeGoods;
import util.CompositeOrder;
import util.JsonResult;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/Order")
public class OrderController {
    @Resource
    private OrderDao orderDao;

    @Resource
    private UserDao userDao;

    @Resource
    private GoodsDao goodsDao;

    private JsonResult jsonResult = new JsonResult();

    @RequestMapping(value = "/show")
    @ResponseBody
    @Transactional
    public Order show(@RequestBody Map<String, Object> map) {
        Order Order = orderDao.selectById((Integer) map.get("id"));
        return Order;
    }

    @RequestMapping(value = "/insertOrder")
    @ResponseBody
    @Transactional
    public JsonResult insertOrder(@RequestBody Map<String, Object> map) {
        int flag = orderDao.insert((Integer) map.get("userId"));
        if (flag == 0) {
            jsonResult.setResult("false");
            return jsonResult;
        } else {
            jsonResult.setResult("true");
            return jsonResult;
        }
    }


    @RequestMapping(value = "/deleteOrder")
    @ResponseBody
    @Transactional
    public JsonResult deleteUser(@RequestBody Map<String, Object> map) {
        int flag = orderDao.delete((Integer) map.get("id"));
        if (flag == 0) {
            jsonResult.setResult("false");
            return jsonResult;
        } else {
            jsonResult.setResult("true");
            return jsonResult;
        }
    }

    @RequestMapping(value = "/showAll")
    @ResponseBody
    @Transactional
    public List<CompositeOrder> showAll() {
        List<CompositeOrder> compositeOrderList = new ArrayList<CompositeOrder>();
        List<Order> orderList = orderDao.selectAll();
        for (Order order : orderList
                ) {
            CompositeOrder tmp = new CompositeOrder();

            tmp.setId(order.getId());

            User user = userDao.selectById((Long) order.getUserId());
            tmp.setUser(user);

            List<CompositeGoods> compositeGoodsList=orderDao.selectCompositeGoods(order.getId());
            tmp.setCompositeGoodsList(compositeGoodsList);

            double total=0 ;
            for (CompositeGoods tmp1: compositeGoodsList
                 ) {
                total+=tmp1.getSum();
            }
            tmp.setSum(total);

            compositeOrderList.add(tmp);

        }
        return compositeOrderList;
    }

    /*订单中添加商品*/
    @RequestMapping(value = "/addgoods")
    @ResponseBody
    @Transactional
    public JsonResult addgoods(@RequestBody Map<String, Object> map) {
        int flag = orderDao.addgoods((Integer) map.get("orderID"), (Integer) map.get("goodsID"), (Integer) map.get("number"));
        if (flag == 0) {
            jsonResult.setResult("false");
            return jsonResult;
        } else {
            jsonResult.setResult("true");
            return jsonResult;
        }
    }

    /*修改订单中某商品数量*/
    @RequestMapping(value = "/modifyOrderNum")
    @ResponseBody
    @Transactional
    public JsonResult modifyOrder(@RequestBody Map<String, Integer> map) {
        int flag;
        if (map.get("number") == 0) {
            flag = orderDao.deleteGoods(map.get("goodsID"), map.get("orderID"));
        } else {
            flag = orderDao.updateGoods(map.get("goodsID"), map.get("orderID"), map.get("number"));
        }
        if (flag == 0) {
            jsonResult.setResult("false");
            return jsonResult;
        } else {
            jsonResult.setResult("true");
            return jsonResult;
        }
    }

    /*显示订单中所有商品*/
    @RequestMapping(value = "/showAllgoods")
    @ResponseBody
    @Transactional
    public List<OrderGoods> showAllgoods(@RequestBody Map<String, Integer> map) {
        int flag;
        List<OrderGoods> goodsList = orderDao.showAllgoods(map.get("orderId"));
        return goodsList;

    }

}
